import { type AppVersionVO, type SelectNowVersionVO } from "@/types/AppConfigType";
import { type CheckUpdateVO, IsForce, UpdateMethod } from "./CommonTypes";

export const useCommonServiceHooks = ()=>{
    
    const appUpdate = (appNowVersion: SelectNowVersionVO, checkUpdateVO:CheckUpdateVO)=>{
        
        if (uni.getSystemInfoSync().platform === 'android') { // 安卓
            if(appNowVersion.android.wgtUrl == null || appNowVersion.android.wgtUrl.trim() == ''){
                // 使用apk更新
                androidApkUpload(
                    appNowVersion.android!, 
                    checkUpdateVO.isForce
                );
              }else{
                if(checkUpdateVO.updateMethod == UpdateMethod.APK){
                    // 使用apk更新
                    androidApkUpload(
                        appNowVersion.android!, 
                        checkUpdateVO.isForce
                    );
                }else if(checkUpdateVO.updateMethod == UpdateMethod.WGT){
                    // 使用wgt更新
                    androidWgtUpload(
                        appNowVersion.android!, 
                        checkUpdateVO.isForce
                    );
                }
            }
        }else{ // ios
            if(appNowVersion.ios.wgtUrl == null || appNowVersion.ios.wgtUrl.trim() == ''){
                // 使用apk更新
                iosApkUpload(
                    appNowVersion.ios!, 
                    checkUpdateVO.isForce
                );
              }else{
                if(checkUpdateVO.updateMethod == UpdateMethod.APK){
                    // 使用apk更新
                    iosApkUpload(
                        appNowVersion.ios!, 
                        checkUpdateVO.isForce
                    );
                }else if(checkUpdateVO.updateMethod == UpdateMethod.WGT){
                    // 使用wgt更新
                    iosWgtUpload(
                        appNowVersion.ios!, 
                        checkUpdateVO.isForce
                    );
                }
            }
        }
    }

    const androidApkUpload = (appVersionVO:AppVersionVO, isForce:IsForce)=>{
        uni.showModal({
            title: '提示',
            content: '发现有新版本(v'+appVersionVO.versionStr+')可以升级\r\n' + appVersionVO.info,
            cancelText: '取消更新',
            showCancel: (isForce == IsForce.yes ? false : true),
            confirmText: '立即更新',
            success: res1 => {
                if (res1.confirm) {
                    console.log('用户点击确定');

                    plus.runtime.openURL(appVersionVO.fileUrl);
                    
                } else if (res1.cancel) {
                    console.log('用户点击取消');
                }
            },
            fail: (err) => {
                console.log('下载失败', err);
            }
        })
    }

    const iosApkUpload = (appVersionVO:AppVersionVO, isForce:IsForce)=>{
        uni.showModal({
            title: '提示',
            content: '发现有新版本(v'+appVersionVO.versionStr+')可以升级\r\n' + appVersionVO.info,
            cancelText: '取消更新',
            showCancel: (isForce == IsForce.yes ? false : true),
            confirmText: '立即更新',
            success: res1 => {
                if (res1.confirm) {
                    console.log('用户点击确定');

                    plus.runtime.openURL(appVersionVO.fileUrl);
                    
                } else if (res1.cancel) {
                    console.log('用户点击取消');
                }
            },
            fail: (err) => {
                console.log('下载失败', err);
            }
        })
    }

    const androidWgtUpload = (appVersionVO:AppVersionVO, isForce:IsForce)=>{
        uni.showModal({
            title: '提示',
            content: '发现有新版本(v'+appVersionVO.versionStr+')可以升级\r\n' + appVersionVO.info,
            cancelText: '取消更新',
            showCancel: (isForce == IsForce.yes ? false : true),
            confirmText: '立即更新',
            success: res1 => {
                if (res1.confirm) {
                    console.log('用户点击确定');

                    uni.showToast({
                        title: '更新中...',
                        duration: 6 * 1000 * 10,
                        icon: 'none'
                    });
                    uni.downloadFile({
                        url: appVersionVO.wgtUrl,
                        timeout: 300000,
                        success: (downloadResult) => {
                            if (downloadResult.statusCode === 200) {
                                plus.runtime.install(
                                    downloadResult.tempFilePath, {
                                        force: false
                                    },
                                    () => {
                                        uni.hideToast();
                                        plus.nativeUI.toast('最新版本下载完成')
                                        plus.runtime.restart();
                                    }, (e) => {
                                        uni.hideToast();
                                        plus.nativeUI.toast("补丁安装失败")
                                    });
                            }
                        },
                        fail: (err) => {
                            console.error(err)
                            uni.hideToast();
                            plus.nativeUI.toast("补丁下载失败")
                        }
                    })

                    
                    
                } else if (res1.cancel) {
                    console.log('用户点击取消');
                }
            },
            fail: (err) => {
                console.log('下载失败', err);
            }
        })
    }

    const iosWgtUpload = (appVersionVO:AppVersionVO, isForce:IsForce)=>{
        uni.showModal({
            title: '提示',
            content: '发现有新版本(v'+appVersionVO.versionStr+')可以升级\r\n' + appVersionVO.info,
            cancelText: '取消更新',
            showCancel: (isForce == IsForce.yes ? false : true),
            confirmText: '立即更新',
            success: res1 => {
                if (res1.confirm) {
                    console.log('用户点击确定');

                    uni.showToast({
                        title: '更新中...',
                        duration: 6 * 1000 * 10,
                        icon: 'none'
                    });
                    uni.downloadFile({
                        url: appVersionVO.wgtUrl,
                        timeout: 300000,
                        success: (downloadResult) => {
                            if (downloadResult.statusCode === 200) {
                                plus.runtime.install(
                                    downloadResult.tempFilePath, {
                                        force: false
                                    },
                                    () => {
                                        uni.hideToast();
                                        plus.nativeUI.toast('最新版本下载完成')
                                        plus.runtime.restart();
                                    }, (e) => {
                                        uni.hideToast();
                                        plus.nativeUI.toast("补丁安装失败")
                                    });
                            }
                        },
                        fail: (err) => {
                            console.error(err)
                            uni.hideToast();
                            plus.nativeUI.toast("补丁下载失败")
                        }
                    })

                    
                    
                } else if (res1.cancel) {
                    console.log('用户点击取消');
                }
            },
            fail: (err) => {
                console.log('下载失败', err);
            }
        })
    }

    return {
        appUpdate
    };
}